<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/2
 * Time: 17:24
 */

namespace App\Admin\Models;

/**
 * App\Admin\Models\UserModel
 *
 * @property int $id
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $name 名称
 * @property string|null $avatar 头像URL
 * @property string|null $remember_token 记住账号密码token
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserModel whereUsername($value)
 * @mixin \Eloquent
 */
class UserModel extends BaseModel
{
    protected $table = 'admin_users';

    protected $primaryKey = 'id';

    CONST TEACHER_ROLE_ID = 2;

    /**
     * 查教师列表
     *
     * @param array $params
     *
     * @return array|\Illuminate\Support\Collection
     */
    public static function getTeacherList(array $params = [])
    {
        $userIds = UserRoleModel::select(['user_id'])->where('role_id', '=', static::TEACHER_ROLE_ID)->get();
        if (empty($userIds)) {
            return [];
        }
        $userIds = json_decode(json_encode($userIds), true);
        $userIds = array_column($userIds, 'user_id');
        if (!empty($params['user_ids']) && is_array($params['user_ids'])) {
            $userIds = array_intersect($userIds, $params['user_ids']);
        }

        $list = static::select(['id', 'name', 'avatar'])->whereIn('id', $userIds)->get();

        return !empty($list) ? json_decode(json_encode($list), true) : [];
    }
}
